package com.hfzy.ihk.web.ivr.biz;

import com.hfzy.ihk.facade.ivr.vo.CallHistory;
import com.hfzy.ihk.web.ivr.constant.Constants;
import com.hfzy.ihk.web.ivr.context.DisruptorInitBean;
import com.hfzy.ihk.web.ivr.context.EventSocketInitBean;
import com.hfzy.ihk.web.ivr.support.callin.event.FreeswitchCallOutEvent;
import com.hfzy.ihk.web.ivr.support.disruptor.DisruptorSupport;
import com.lmax.disruptor.RingBuffer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.Executor;

/**
 * Freeswitch外呼处理业务类
 *
 * @Auther: Administrator
 * @Date: 2018/10/24 14:52
 * @Description:
 */
@Lazy
@Component
public class FreeswitchAsrCallOutBiz extends DisruptorSupport<CallHistory, FreeswitchCallOutEvent> {

    Logger logger = LoggerFactory.getLogger(FreeswitchAsrCallOutBiz.class);
    @Autowired
    private EventSocketInitBean eslInitBean;


    @Override
    protected RingBuffer<FreeswitchCallOutEvent> useRingBuffer(CallHistory data) {
        RingBuffer ringBuffer = null;
        String useType = "ivr Freeswitch呼出";
        ringBuffer = DisruptorInitBean.getRingBufferFreeswitchCallOut();
        checkRingBufferUse(ringBuffer, useType);
        return ringBuffer;
    }


    /**
     * 进队列呼出
     *
     * @param data
     * @throws Exception
     */
    public void todoCallOut(CallHistory data) throws Exception {
        putInRingBuffer(data);
    }


    /**
     * freeswitch 外呼api调用
     *
     * @return
     */
    public Map<String, String> callOutOpt(CallHistory data) {

        Map<String, String> resultMap = new HashMap<>();
        try {

            //提交外呼
            commitFsCpt(data.getSessionId(), data.getMainCallPhone(),data.getShowNum());


            resultMap.put("result", Constants.SUCCESS);
            resultMap.put("msg", "提交外呼成功");

        } catch (Exception e) {
            logger.error("ivr 选线外呼异常!", e);
            resultMap.put("result", Constants.FAIL);
            resultMap.put("msg", "外呼失败，请重试");
        }
        return resultMap;
    }


    private void commitFsCpt(String uuid,String mainCallPhone,String showNum){

        //转入拨号计划
        final String  callParam =  "{origination_uuid=" + uuid +",origination_caller_id_number="+showNum+",origination_caller_id_name="+showNum+"}" +
                "sofia/external/99"+mainCallPhone+"@122.13.6.112:5060 00";
            logger.info("ivr freeswitch本次呼出sessionId为: "+uuid+",参数:" + callParam);


        //使用提交外呼请求
        try {
            logger.info("ivr freeswitch开始提交呼出请求,sessionId为: " + uuid);

            long startTime = System.currentTimeMillis();
            eslInitBean.addCall(uuid);  //将本次通话添加到由这个服务发起的通话map中
            eslInitBean.getClient().sendBackgroundApiCommand("originate", callParam);

            long endTime = System.currentTimeMillis();

            logger.info("ivr freeswitch提交外呼请求完毕,sessionId:" + uuid + ",耗时" + (endTime - startTime) + "毫秒");
        } catch (Exception e) {
            logger.error("ivr freeswitchlog:freeswitch外呼的时候出异常了!", e);
            throw new RuntimeException(e);
        }
    }


}
